from wtforms import StringField, TextAreaField, SelectField
from wtforms.validators import Optional, ValidationError, DataRequired

from apps.forms.base_form import BaseForm
from rules.video_library_rule import search_video_library_by_title


class QueryVideoLibraryForm(BaseForm):
    title = StringField('标题', validators=[Optional()], render_kw={"placeholder": "标题", 'class': 'search-input'})
    dy_open_video_id = StringField('抖音视频id', validators=[Optional()],
                                   render_kw={"placeholder": "抖音视频id", 'class': 'search-input'})
    dy_sync_status = SelectField('同步状态', validators=[Optional()], coerce=str,
                                 render_kw={'class': 'filter-select'},
                                 default='')

    def __init__(self, *args, **kwargs):
        super(QueryVideoLibraryForm, self).__init__(*args, **kwargs)
        self.dy_sync_status.choices = self.get_dy_sync_status_list()


class CreateVideoLibraryForm(BaseForm):
    title = StringField('标题', validators=[DataRequired(message='请填写标题')],
                        render_kw={"placeholder": "请填写标题"})
    file_name = StringField('视频素材', validators=[DataRequired(message='请上传视频')])
    temp_file_name = StringField('临时文件名', validators=[DataRequired(message='请上传视频')])
    remarks = TextAreaField('备注', validators=[Optional()])

    def validate_title(self, field):
        model_orm = search_video_library_by_title(field.data)
        if model_orm:
            raise ValidationError('标题已存在')


class UpdateVideoLibraryForm(BaseForm):
    title = StringField('标题', validators=[DataRequired(message='请填写标题')],
                        render_kw={"placeholder": "请填写标题"})
    file_name = StringField('视频名称', validators=[DataRequired(message='请上传视频')])
    remarks = TextAreaField('备注', validators=[Optional()])

    def __init__(self, model_orm=None, *args, **kwargs):
        super(UpdateVideoLibraryForm, self).__init__(*args, **kwargs)
        if model_orm:
            self.title.data = model_orm.title
            self.file_name.data = model_orm.file_name
            self.remarks.data = model_orm.remarks
